--[[
* MapNotesShare 2.0
* File: MapNotesShare\MapNotesShare.lua
* $Revision: 136 $
* $Date: 2007-01-06 13:14:22 +0000 (Sat, 06 Jan 2007) $
* 
* Copyright (c) 2006-2007, Mery (Moritz Metelmann)
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]--

-- Check for required AddOns
-- (They are opt.dep because only one is needed to run.)
if (not MapNotes) then return; end;

if (not MapNotesShare) then MapNotesShare = {}; end;

local L = MapNotesShare.Localization;

MapNotesShare.OpenShareMenu = function(poi)
	if (poi.modID == "MAPNOTES") then
		-- set name,
		local note = MapNotesSaved_NoteDB[poi.id];
		MapNotesShareNameText:SetText(note.title);
--~ 		DevTools_Dump(poi);
		-- set icon max size 24,
		local icon = MapNotes.Icon.GetIconTexture(note.iconID, note.category);
		local texture = MapNotesShareNameIcon;
		texture:SetTexture(icon.file);
		if (icon.size < 24) then
			texture:SetWidth(icon.size); texture:SetHeight(icon.size);
		else
			texture:SetWidth(24); texture:SetHeight(24);
		end;
		if (icon.tcoordMinX) then texture:SetTexCoord(icon.tcoordMinX, icon.tcoordMaxX, icon.tcoordMinY, icon.tcoordMaxY); else texture:SetTexCoord(0, 1, 0, 1); end;
		if (icon.colorR) then texture:SetVertexColor(icon.colorR, icon.colorG, icon.colorB, icon.colorA); else texture:SetVertexColor(1, 1, 1, 1); end;

		-- set modname
		MapNotesShareAddonText:SetText("MapNotes");
		-- set zone and zoneID
		MapNotesShareZoneText:SetText(MapLib.mapInfo[note.zone].mapName);
		MapNotesShareZoneIDText:SetText(note.zone);
		-- set x, y
		MapNotesShareXText:SetText(string.format("%.2f", note.x*100));
		MapNotesShareYText:SetText(string.format("%.2f", note.y*100));
		-- playername ""
		MapNotesShareEditBox:SetText("");
		-- show
		MapNotesShareFrame:Show();
		-- set focus to editbox
		MapNotesShareEditBox:SetFocus();
	end;
	
	MapNotesShareFrame.modID = poi.modID;
	MapNotesShareFrame.noteID = poi.id;
end;

MapNotesShare.SendNote = function()
	-- DevTools_Dump("Now I should be sending note '"..MapNotesShareFrame.noteID.."' from the AddOn '"..MapNotesShareFrame.modID.."' to the player with the name '"..MapNotesShareEditBox:GetText().."'.");
	local noteTable;
	if (MapNotesShareFrame.modID == "MAPNOTES") then
		local note = MapNotesSaved_NoteDB[MapNotesShareFrame.noteID];
		local titleColor, descriptionColor, _, _, _ = strsplit("$", note.misc);
		noteTable ={
			modID = "MAPNOTES";
			title = note.title,
			description = note.description,
			titleColor = titleColor,
			descriptionColor = descriptionColor,
			x = note.x,
			y = note.y,
			zone = note.zone,
			iconID = note.iconID,
		};
	end;
--~ 	DevTools_Dump(noteTable);
	Telepathy.sendTable("MapNotes$hare", noteTable, "WHISPER", MapNotesShareEditBox:GetText());
	MapNotesShareFrame:Hide();
end;

MapNotesShare.ReceiveNote = function(msg, sender)
	if (msg.modID == "MAPNOTES" and MapNotes) then
		if (not MapLib.mapInfo[msg.zone]) then 
			MapLib.ChatHandler.PrintToChat(string.format(L["Could not receive a note from %s on the map of %s. (Error: You don't know this zone.)"], sender, msg.zone));
			return;
		end;
		local gotIt = MapNotes.CreateNote(msg.zone, msg.x, msg.y, msg.title, msg.titleColor, MapNotes.Localization["Received Notes"], msg.iconID, msg.description, msg.descriptionColor, sender);
		if (gotIt) then
			MapLib.ChatHandler.PrintToChat(string.format(L["Received a note from %s on the map of %s."], sender, msg.zone));
		else
			MapLib.ChatHandler.PrintToChat(string.format(L["Could not receive a note from %s on the map of %s. (Error: A note already exsists in that position.)"], sender, msg.zone));
		end;
	else
		MapLib.ChatHandler.PrintToChat(string.format(L["Could not receive a note from %s on the map of %s. (Error: The mod '%s' wasn't found.)"], sender, msg.zone, msg.modID));
	end;
end;

MapNotesShare.OnShow = function()
	MapLib.Block.Request("MapNotesShareMenu", function() end, MAPNOTES_FRAMELEVEL_EDITMENU-1);
end;

MapNotesShare.OnHide = function()
	MapLib.Block.Release("MapNotesShareMenu");
end;

MapLib.POI.RegisterMenuButton(L["Share Note"], MapNotesShare.OpenShareMenu, {"MAPNOTES"}, function() return true; end);
Telepathy.registerListener("MapNotes$hare", "WHISPER", MapNotesShare.ReceiveNote);